import { Module } from 'vuex'

import {
  getCountUp,
  downloadCSV,
  downloadMCSV,
  getMellaUVByFilter,
  getMellaMUVByFilter
} from '@/service/main/general/general'

import { IStatistics } from './type'
import { IRootState } from '../../type'

const generalModule: Module<IStatistics, IRootState> = {
  namespaced: true,
  state() {
    return {
      topPanelDatas: [],
      mellaUV: {},
      mellaMUV: {}
    }
  },
  mutations: {
    changeTopPanelDatas(state, list) {
      state.topPanelDatas = list
    },
    changeMellaUV(state, list) {
      state.mellaUV = list
    },
    changeMellaMUV(state, list) {
      state.mellaMUV = list
    }
  },
  actions: {
    async changeDayLengthAction({ commit }, info: any) {
      if (info.pageName == 'uvPage') {
        const mellaUV: any = await getMellaUVByFilter(info.value)
        commit('changeMellaUV', mellaUV.data.data)
      } else if (info.pageName == 'muvPage') {
        const mellaMUV: any = await getMellaMUVByFilter(info.value)
        commit('changeMellaMUV', mellaMUV.data.data)
      }
    },
    async getAnalysisDataAction({ commit }) {
      const resultTopPanelDatas: any = await getCountUp()
      commit('changeTopPanelDatas', resultTopPanelDatas.data.data)

      const mellaUV: any = await getMellaUVByFilter('30')
      commit('changeMellaUV', mellaUV.data.data)

      const mellaMUV: any = await getMellaMUVByFilter('30')
      commit('changeMellaMUV', mellaMUV.data.data)
    },
    async downloadCSVAction() {
      downloadCSV()
    },
    async downloadMCSVAction() {
      downloadMCSV()
    }
  }
}

export default generalModule
